#include <jni.h>
#include <fcntl.h>
#include <unistd.h>


#include <android/log.h>
#include "../i2c/main.h"
#define TAG "MyJNITag"  // 自定义日志标签
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#include <errno.h>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_bus_AndroidI2cJniControl_test(
        JNIEnv* env, jclass clazz) {
    int result = test()    ;
    char buffer[20];
    snprintf(buffer, sizeof(buffer), "%d", result); // 整数转字符串
    return env->NewStringUTF(buffer);
}

extern "C" JNIEXPORT void JNICALL
Java_com_example_bus_AndroidI2cJniControl_initDevice(
        JNIEnv* env, jclass clazz) {
    LOGD("Entering initDevice");
    int result = initDevice()    ;
    LOGD("init result: %d", result);
}

extern "C" JNIEXPORT jdoubleArray  JNICALL
Java_com_example_bus_AndroidI2cJniControl_getBME28Value(
        JNIEnv* env, jclass clazz) {
    double* BME280Value = getBme280Value();

    jdoubleArray result = env->NewDoubleArray(3);
    if (result == nullptr) {
        return nullptr;
    }

    env->SetDoubleArrayRegion(result, 0, 3, BME280Value);
    return result;
}

extern "C" JNIEXPORT jfloatArray JNICALL
Java_com_example_bus_AndroidI2cJniControl_getImuValue(
        JNIEnv* env, jclass clazz) {
    float * value =getImuValue();
    jfloatArray result = env->NewFloatArray(13);
    if (result == nullptr) {
        return nullptr;
    }
    env->SetFloatArrayRegion(result, 0, 13, value);
    return result;
}